<?php

/*
 * Emailer Class
 * ===============================================
 * Date: May 20th, 2012
 * Created By: Integration Team
 * ===============================================
 */

/**
 * Emailing class - used as a wrapper for the 
 * class.phpmailer.php class 
 * picks the email addresses from the config file
 * 
 */

require_once 'email/class.phpmailer.php';
require_once 'customExceptionClass.php';

class eMailer {
    
    protected $SMTPsecure;
    protected $host;
    protected $port;
    protected $SMTP_AUTH;
    protected $userName;
    protected $password;
    protected $fromAddress;
    protected $fromName;
    protected $subject;
    protected $emailAddresses; //To email(s)
    protected $emailBody;


    public function __construct($body="") 
    {
        //Check if the email configuration is set in the config.php file
        
        if(defined('SMTP_Secure'))
        {
            $this->SMTPsecure = SMTP_Secure;
        }
        else
        {
            $this->SMTPsecure = 'ssl';
        }
        if(defined('SMTP_HOST'))
        {
            $this->host = SMTP_HOST;
        }
        else
        {
            $this->host = 'mail.gmail.com';
        }
        if(defined('SMTP_PORT'))
        {
            $this->port = SMTP_PORT;
        }
        else
        {
            $this->port = 465; //465 for SSL and 587 for TLS
        }
        if(defined('SMTP_AUTH'))
        {
            $this->SMTP_AUTH = SMTP_AUTH;
        }
        else
        {
            $this->SMTP_AUTH = TRUE;
        }
        if(defined('Email_Username'))
        {
            $this->userName = Email_Username;
        }
        else
        {
            $this->userName = 'etilize.integration@gmail.com';
        }
        if(defined('Email_Password'))
        {
            $this->password = Email_Password;
        }
        else
        {
            $this->password = '1nt3gr@t3';
        }
        if(defined('Email_From_Address'))
        {
            $this->fromAddress = Email_From_Address;
        }
        else
        {
            $this->fromAddress = 'etilize.integration@gmail.com';
        }
        if(defined('Email_From_Name'))
        {
            $this->fromName = Email_From_Name;
        }
        else
        {
            $this->fromName = 'PHP Export Script';
        }
        if(defined('Email_Subject'))
        {
            $this->subject = Email_Subject;
        }
        else
        {
            $this->subject = 'PHP Export Script';
        }
        if(defined('Email_Addresses'))
        {
            $this->emailAddresses = Email_Addresses;
        }
        else
        {
            $this->Email_Addresses = 'integrate@etilize.com';
        }
    }
    
    public function sendEmail($emailBody)
    {
        $mailer = new PHPMailer;
        $mailer->IsSMTP();
        $mailer->SMTPSecure = $this->SMTPsecure;
        $mailer->Host = $this->host;
        $mailer->Port = $this->port;
        $mailer->SMTPAuth = $this->SMTP_AUTH;
        $mailer->Username = $this->userName;
        $mailer->Password = $this->password;
        $mailer->From = $this->fromAddress;
        $mailer->FromName = $this->fromName;
        $mailer->Subject = $this->subject;
        $mailer->IsHTML();
        $mailer->Body = $emailBody;
        
        
        if($this->emailAddresses=="")
        {
            Throw new Exception('No Email specified',2);
        }
        else
        {
            $addresses = explode(',', $this->emailAddresses);
            foreach($addresses as $email)
            {
                $mailer->AddAddress($email);
     
            }
                
                    if(!$mailer->Send())
                    {
                        Throw new customException("Could not send email to email address");
                    }
                
        }
    }
    
}

?>
